/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controladores;

import controladores.exceptions.NonexistentEntityException;
import controladores.exceptions.PreexistingEntityException;
import entidades.Reservatransporte;
import entidades.ReservatransportePK;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import entidades.Reserva;

/**
 *
 * @author Andres
 */
public class ReservatransporteJpaController {

    public ReservatransporteJpaController() {
        emf = Persistence.createEntityManagerFactory("proyectoCongresoPU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(Reservatransporte reservatransporte) throws PreexistingEntityException, Exception {
        if (reservatransporte.getReservatransportePK() == null) {
            reservatransporte.setReservatransportePK(new ReservatransportePK());
        }
        reservatransporte.getReservatransportePK().setReservaCodigoreserva(reservatransporte.getReserva().getCodigoreserva());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reserva reserva = reservatransporte.getReserva();
            if (reserva != null) {
                reserva = em.getReference(reserva.getClass(), reserva.getCodigoreserva());
                reservatransporte.setReserva(reserva);
            }
            em.persist(reservatransporte);
            if (reserva != null) {
                reserva.getReservatransporteList().add(reservatransporte);
                reserva = em.merge(reserva);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            if (findReservatransporte(reservatransporte.getReservatransportePK()) != null) {
                throw new PreexistingEntityException("Reservatransporte " + reservatransporte + " already exists.", ex);
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(Reservatransporte reservatransporte) throws NonexistentEntityException, Exception {
        reservatransporte.getReservatransportePK().setReservaCodigoreserva(reservatransporte.getReserva().getCodigoreserva());
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reservatransporte persistentReservatransporte = em.find(Reservatransporte.class, reservatransporte.getReservatransportePK());
            Reserva reservaOld = persistentReservatransporte.getReserva();
            Reserva reservaNew = reservatransporte.getReserva();
            if (reservaNew != null) {
                reservaNew = em.getReference(reservaNew.getClass(), reservaNew.getCodigoreserva());
                reservatransporte.setReserva(reservaNew);
            }
            reservatransporte = em.merge(reservatransporte);
            if (reservaOld != null && !reservaOld.equals(reservaNew)) {
                reservaOld.getReservatransporteList().remove(reservatransporte);
                reservaOld = em.merge(reservaOld);
            }
            if (reservaNew != null && !reservaNew.equals(reservaOld)) {
                reservaNew.getReservatransporteList().add(reservatransporte);
                reservaNew = em.merge(reservaNew);
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                ReservatransportePK id = reservatransporte.getReservatransportePK();
                if (findReservatransporte(id) == null) {
                    throw new NonexistentEntityException("The reservatransporte with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(ReservatransportePK id) throws NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            Reservatransporte reservatransporte;
            try {
                reservatransporte = em.getReference(Reservatransporte.class, id);
                reservatransporte.getReservatransportePK();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The reservatransporte with id " + id + " no longer exists.", enfe);
            }
            Reserva reserva = reservatransporte.getReserva();
            if (reserva != null) {
                reserva.getReservatransporteList().remove(reservatransporte);
                reserva = em.merge(reserva);
            }
            em.remove(reservatransporte);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<Reservatransporte> findReservatransporteEntities() {
        return findReservatransporteEntities(true, -1, -1);
    }

    public List<Reservatransporte> findReservatransporteEntities(int maxResults, int firstResult) {
        return findReservatransporteEntities(false, maxResults, firstResult);
    }

    private List<Reservatransporte> findReservatransporteEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(Reservatransporte.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public Reservatransporte findReservatransporte(ReservatransportePK id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(Reservatransporte.class, id);
        } finally {
            em.close();
        }
    }

    public int getReservatransporteCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<Reservatransporte> rt = cq.from(Reservatransporte.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
